<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
  <head>
    <title>教材吧-添加订单</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<%@ include file="/base/jsp/includeweb.jsp"%>
    <link type="text/css" rel="stylesheet" href="${path}/base/css/order.css"/>
  </head>
  <body>
  <%@ include file="/base/jsp/header.jsp"%>
  <%@ include file="/base/jsp/search-header.jsp"%>
  <div id="content-wrapper" class="content-wrapper border-top-org clear">
    <h2>填写收件人信息</h2>
    <div class="add-order-wrapper clear">
    <form id="orderForm">
        <div class="line-wrapper clear">
            <div class="line-info"><span class="color-red">*</span>收件人姓名：</div>
            <div class="line-input"><input name="name" req="true" type="text" class="order-input"/></div>
        </div> 
        <div class="line-wrapper clear">
           <div class="line-info"><span class="color-red">*</span>收件人联系方式：</div>
           <div class="line-input"><input name="phone" req="true" type="text" class="order-input"/></div>
        </div>
        <div class="line-wrapper clear">
           <div class="line-info"><span class="color-red">*</span>收件人地址：</div>
           <div class="line-input">
              <ul id="order-school-input" class="search-bottom-input"><span class="major-text">学校</span></ul>
              <!--<ul id="depart-input" class="search-bottom-input"><span class="major-text">校区</span><li>仙林</li><li>鼓楼</li></ul>-->
                                               校区：<input name="campus" req="true" type="text" class="order-input order-input-shorter"/>                                
                                               宿舍楼：<input name="dormitory" req="true" type="text" class="order-input order-input-shorter"/>
                                               宿舍号：<input name="dormitory_num" req="true" type="text" class="order-input order-input-shorter"/>
            </div>
        </div>
         <div class="line-wrapper clear">
           <div class="line-info"><span class="color-red">*</span>收货方式：</div>
           <div class="line-input"><input name="type" req="true" type="radio" class="order-radio" checked="true"/>货到付款</div>
        </div>
         <div class="line-but-wrapper clear">
         <input id="order-submit" type="button" value="提交订单" class="but-org"/>
         </div>
         <input type="text" class="hide" name="userid" id="userid"/>
         <input type="text" class="hide" name="bookid" id="bookid"/>
    </form>
    </div>
  </div>
  <%@ include file="/base/jsp/footer.jsp"%>
  </body>
  <script type="text/javascript">
  var bookid = getParameter("bookid");
  var userid = getParameter("userid");
  
  $(function(){
     $("input[name=userid]").val(userid);
     $("input[name=bookid]").val(bookid);
     request("listSchool.do","pageIndex=0&pageSize=100&isNotPage=0",function(data){
                var d = data.data;
                $("#order-school-input li").remove();
                for(var i =0 ; i < d.length; i++){
                    var rowd = $("<li></li>");
                    var dt = d[i];
                    if(dt.name!='所有'){
                       rowd.attr("id",dt.id);
                       rowd.text(dt.name);
                       rowd.attr("code",dt.code);
                       $("#order-school-input").append(rowd);
                    }
                }
                $("#order-school-input li").click(function(){
                    var s = $(this);
                    s.parent().find("span").eq(0).text(s.text());
                    s.parent().find("span").eq(0).attr("id",s.attr("id"));
                    s.parent().find("span").eq(0).attr("code",s.attr("code"));
                    s.parent().find("li").hide();
                });
                //再去请求用户信息
                requestWithoutCheck("findByIdUser.do","id="+userid,function(d){
                    var s = $("#order-school-input").find("span").eq(0);
                    s.text(d.sName);
                    s.attr("id",d.sid);
                    s.attr("code",d.sCode);
                    var addr = d.address;
                    var ads = addr.split("-");
                    $("input[name=campus]").val(ads[0]);
                    $("input[name=dormitory]").val(ads[1]);
                    $("input[name=dormitory_num]").val(ads[2]);
                    $("input[name=name]").val(d.name);
                    $("input[name=phone]").val(d.phone);
                });
      });
     
    
     
     $("#order-submit").click(function(){
         var s = $("#order-school-input").find("span").eq(0);
         var sid = s.attr("id");
         var sCode = s.attr("code");
         var sName = s.text();
         var campus=  $("input[name=campus]").val();
         var dormitory = $("input[name=dormitory]").val();
         var dormitory_num =  $("input[name=dormitory_num]").val();
         var name = $("input[name=name]").val();
         var phone = $("input[name=phone]").val();
         bookid = $("input[name=bookid]").val();
         userid = $("input[name=userid]").val();
         if(sName == '学校' || campus == '' || dormitory == '' || dormitory_num == '' || name == ''|| phone == '' || userid == '' || bookid == ''){
             alert("请输入完整的信息");
             return;
         }
         var address = campus+"-"+dormitory+"-"+dormitory_num;
         var queryUser = "sid="+sid+"&sCode="+sCode+"&sName="+sName+"&address="+address+"&name="+name+"&phone="+phone+"&id="+userid;
         var query = "sid="+sid+"&sCode="+sCode+"&sName="+sName+"&address="+address+"&name="+name+"&phone="+phone+"&bookids="+bookid+"&userid="+userid;
         //alert(query);
         //首先请求保存用户信息
         requestWithoutCheck("saveUser.do",queryUser,function(data){
                requestWithoutCheck("saveOrder.do",query,function(d){
                   var result = d.data.success;
                   var fails = d.data.carts;
                   var books = "";
                   for(var k in fails){
                       var fail = fails[k];
                       books += fail.bookName+"，";
                   }
                   books = books.substring(0,books.length - 1);
                   if(result){
                      if(books != ""){
                         alert('"'+books+'"库存不足，提交失败');
                      }else{
                         alert("提交成功");
                      }
                      location.href="weborders.do?isM=1&userid=${sessionScope.userid}";
                   }else{
                       alert('"'+books+'"库存不足，提交失败');
                   }
                });
          });
     })
  
  })
  
  
  
  </script>
</html>